﻿#include "Widget.h"
#include <WebEngineView.h>

Widget::Widget(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::WidgetClass())
{
	ui->setupUi(this);
	m_webEngineView = new WebEngineView(ui->widget);
	m_webEngineView->load(QUrl("qrc:/resource/index.html"));
}

Widget::~Widget()
{
	delete ui;
}

void Widget::on_devBtn_clicked()
{
	m_webEngineView->openDevTool();
}

void Widget::resizeEvent(QResizeEvent* ev)
{
	QWidget::resizeEvent(ev);
	m_webEngineView->resize(ui->widget->size());
}

void Widget::on_pushButton_clicked()
{
	QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
	WebEngineView::instance()->page()->runJavaScript("add(1,2)",
		[=](const QVariant& var) {
			QString retStr = var.toString();
			qDebug() << "ret:" << retStr;
			if (loop->isRunning()) {
				qDebug() << "bgein loop quit" << retStr;
				loop->quit();
				qDebug() << "end loop quit" << retStr;
			}
		});
	loop->exec(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents);
}
